﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalcSharp.MDIUI
{
    public partial class HistoryWindow : Form
    {

        private Dictionary<string, DateTime> History;
        private int cmdindex = -1;
        string[] cmds;
 
        public HistoryWindow()
        {
            InitializeComponent();
            History = new Dictionary<string, DateTime>();
        }

        public void AddHistoryItem(string p)
        {
            if (History.ContainsKey(p)) History[p] = DateTime.Now;
            else History.Add(p, DateTime.Now);
            if (History.Count > 1)
            {
                var q = from rec in History orderby rec.Value ascending select rec;
                this.History = q.ToDictionary(item => item.Key, item => item.Value);
            }
            cmdindex = History.Count - 1;
            cmds = History.Keys.ToArray();
        }

        private string DisplayDateTime(DateTime dt)
        {
            DateTime n = DateTime.Now;
            StringBuilder txt = new StringBuilder();
            if (dt.Year != n.Year || dt.DayOfYear != n.DayOfYear)
            {
                txt.Append(dt.Year);
                txt.Append("/");
                txt.Append(dt.Month);
                txt.Append("/");
                txt.Append(dt.Day);
                txt.Append(" ");
            }
            txt.Append(dt.Hour);
            txt.Append(":");
            txt.Append(dt.Minute);
            txt.Append(":");
            txt.Append(dt.Second);
            return txt.ToString();
        }

        private void HistoryWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            e.Cancel = true;
        }

        private void HistoryWindow_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.dgv.Rows.Clear();
                foreach (string key in History.Keys)
                {
                    this.dgv.Rows.Add(DisplayDateTime(History[key]), key);
                }
            }
        }

        private void HistoryWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.Enter:
                    string insert = this.dgv.SelectedRows[0].Cells[1].Value.ToString();
                    Program.ConsoleForm.AddCmdBoxText("");
                    Program.ConsoleForm.AddCmdBoxText(insert);
                    this.Close();
                    break;
            }
        }

        public string GetPreviousCommand()
        {
            if (cmds == null) return "";
            if (cmdindex - 1 < 0) cmdindex = History.Count;
            return cmds[--cmdindex];
        }

        public string GetNextCommand()
        {
            if (cmds == null) return "";
            if (cmdindex + 1 > (History.Count - 1)) cmdindex = -1;
            return cmds[++cmdindex];
        }
    }
}
